﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiduRules
{
    public class Validator<T>
    {
        private readonly List<IValidationRule<T>> _rules;

        public Validator()
        {
            _rules = new List<IValidationRule<T>>();
        }

        public void AddRule(IValidationRule<T> rule)
        {
            _rules.Add(rule);
        }

        public ValidationResult Validate(T value)
        {
            var result = new ValidationResult();

            foreach (var rule in _rules)
            {
                if (!rule.IsValid(value))
                {
                    result.IsValid = false;
                    result.Errors.Add(rule.ErrorMessage);
                }
            }

            return result;
        }

        public ValidationResult ValidateStep(T value)
        {
            var result = new ValidationResult();

            foreach (var rule in _rules)
            {
                if (!rule.IsValid(value))
                {
                    result.IsValid = false;
                    result.Errors.Add(rule.ErrorMessage);
                    break;
                }

                result.IsValid = true;
                result.Messages.Add("通过");
                
            }

            return result;
        }
    }

}
